using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using GAS.Common.DataSets;
using GAS.Common.Data;
using GAS.Common.DataSets.AuditDataSetTableAdapters;
using System.Reflection;
using System.Configuration;
using GAS.Common.DbAccess;

namespace GAS.Common.Managers
{
	public class GASManager: DataSetManager
	{
		public void SaveAudit(AuditDataSet auditDS)
		{
			GAS_AUDITTableAdapter auditTA;
			GAS_AUDIT_TYPESTableAdapter auditTypesTA;
			AuditDataSet auditDSChanges;

			#region Prepare IDs

			// AUDIT and AUDIT_TYPES
			PrepareDataSetIDs(Assembly.GetExecutingAssembly(), auditDS.GAS_AUDIT);

			#endregion

			auditDSChanges = (AuditDataSet)auditDS.GetChanges();

			auditTA = new GAS_AUDITTableAdapter();
			auditTypesTA = new GAS_AUDIT_TYPESTableAdapter();

			auditTA.Update(auditDSChanges);
			auditTypesTA.Update(auditDSChanges);
		}

		public void SaveHostAudit(AuditDataSet auditDS, string auditCode)
		{
			GAS_HOST_AUDITTableAdapter hostAuditTA;
			GAS_USERSTableAdapter usersTA;
			GAS_GROUPSTableAdapter groupsTA;
			GAS_USER_GROUPSTableAdapter userGroupsTA;
			GAS_GROUP_PRIVILEGETableAdapter groupPrivilegeTA;
			GAS_LOGSTableAdapter logsTA;
			GAS_SOFTWARE_APPSTableAdapter softwareAppsTA;
			GAS_NET_ADAPTERSTableAdapter netAdaptersTA;
			GAS_HW_DEVICESTableAdapter hwDevicesTA;
			GAS_PROCESSESTableAdapter processesTA;
			GAS_OPEN_PORTSTableAdapter openPortsTA;
			GAS_SECURITY_SETTINGSTableAdapter securitySettingsTA;
			GAS_SO_UPDATESTableAdapter soUpdatesTA;
			AuditDataSet auditDSChanges;

			#region Prepare HOST_AUDIT table

			AuditDataSet.GAS_HOST_AUDITRow hostAuditRow;

			if (auditDS.GAS_HOST_AUDIT.Rows.Count != 1)
			{
				throw new Exception("Incorrect Format. GAS_HOST_AUDIT must contain one and only one element");
			}

			hostAuditRow = (AuditDataSet.GAS_HOST_AUDITRow)auditDS.GAS_HOST_AUDIT.Rows[0];
			hostAuditRow.AUDIT_DATETIME = DateTime.Now;
			hostAuditRow.AUDIT_ID = GetAuditId(auditCode);

			#endregion

			#region Prepare IDs

			// HOST_AUDIT and all other tables
			PrepareDataSetIDs(Assembly.GetExecutingAssembly(), auditDS.GAS_HOST_AUDIT);

			#endregion

			auditDSChanges = (AuditDataSet)auditDS.GetChanges();

			hostAuditTA = new GAS_HOST_AUDITTableAdapter();
			usersTA = new GAS_USERSTableAdapter();
			groupsTA = new GAS_GROUPSTableAdapter();
			userGroupsTA = new GAS_USER_GROUPSTableAdapter();
			groupPrivilegeTA = new GAS_GROUP_PRIVILEGETableAdapter();
			logsTA = new GAS_LOGSTableAdapter();
			softwareAppsTA = new GAS_SOFTWARE_APPSTableAdapter();
			netAdaptersTA = new GAS_NET_ADAPTERSTableAdapter();
			hwDevicesTA = new GAS_HW_DEVICESTableAdapter();
			processesTA = new GAS_PROCESSESTableAdapter();
			openPortsTA = new GAS_OPEN_PORTSTableAdapter();
			securitySettingsTA = new GAS_SECURITY_SETTINGSTableAdapter();
			soUpdatesTA = new GAS_SO_UPDATESTableAdapter();

			hostAuditTA.Update(auditDSChanges);
			usersTA.Update(auditDSChanges);
			groupsTA.Update(auditDSChanges);
			userGroupsTA.Update(auditDSChanges);
			groupPrivilegeTA.Update(auditDSChanges);
			logsTA.Update(auditDSChanges);
			softwareAppsTA.Update(auditDSChanges);
			netAdaptersTA.Update(auditDSChanges);
			hwDevicesTA.Update(auditDSChanges);
			processesTA.Update(auditDSChanges);
			openPortsTA.Update(auditDSChanges);
			securitySettingsTA.Update(auditDSChanges);
			soUpdatesTA.Update(auditDSChanges);
		}

		private decimal GetAuditId(string auditCode)
		{
			GAS_AUDITTableAdapter auditTA;
			AuditDataSet.GAS_AUDITDataTable auditTable;

			auditTA = new GAS_AUDITTableAdapter();

			auditTable = auditTA.GetDataByGUID(auditCode);

			if (auditTable.Rows.Count != 1)
				throw new Exception("The audit code does not exists. You should generate it with GAS tool");
			else
				return ((AuditDataSet.GAS_AUDITRow)auditTable.Rows[0]).ID;
		}

		public string GetLastAuditCode()
		{
			GAS_AUDITTableAdapter auditTA;
			AuditDataSet.GAS_AUDITDataTable auditTable;

			auditTA = new GAS_AUDITTableAdapter();
			auditTable = auditTA.GetLastAuditGuid();

			if (auditTable.Rows.Count != 1)
				throw new Exception("Internal error. Review Database.");
			else
				return ((AuditDataSet.GAS_AUDITRow)auditTable.Rows[0]).GUID;
		}

		public string[] GetAuditOptions(string auditCode)
		{
			GAS_AUDIT_TYPESTableAdapter auditTypesTA;
			AuditDataSet.GAS_AUDIT_TYPESDataTable auditTypesTable;
			string[] auditOptions;

			auditTypesTA = new GAS_AUDIT_TYPESTableAdapter();
			auditTypesTable = auditTypesTA.GetDataByAuditGuid(auditCode);

			if (auditTypesTable.Rows.Count > 0)
			{
				auditOptions = new string[auditTypesTable.Rows.Count];
				int i = 0;
				foreach (AuditDataSet.GAS_AUDIT_TYPESRow auditTypeRow in auditTypesTable.Rows)
				{
					auditOptions[i] = auditTypeRow.AUDIT_TYPE;
					i++;
				}

				return auditOptions;
			}
			else
				return null;
		}

		public DataSet ExecuteSql(string sqlString)
		{
			OracleMicrosoftAccess oracleAccess;
			IDbConnection connection;
			DataSet ds;

			oracleAccess = new OracleMicrosoftAccess(ConfigurationManager.AppSettings["OracleConnectionString"]);
			connection = oracleAccess.GetConnection();
			ds = new DataSet();
			oracleAccess.FillDataSet(connection, ds, sqlString, "Results");

			return ds;
		}
	}
}
